﻿Imports EstoqueDAL
Imports Conection
Public Class ClassificacaoFiscalBLL
    Dim objDAL As ClassificacaoFiscalDAL
#Region "Propriedades"
    Property clf_codigo As Integer
    Property clf_descricao As String
    Property sta_codigo As Integer
#End Region

#Region "Construtores"
    Sub New()

    End Sub
    Sub New(ByVal codigo As Integer)
        carrega(codigo)
    End Sub
    Sub New(codigo As Integer, descricao As String, status As Integer)
        Me.clf_codigo = codigo
        Me.clf_descricao = descricao
        Me.sta_codigo = status
    End Sub
#End Region

#Region "Funções"
#Region "Inserir"
    Function inserir(usuario As Integer) As Boolean
        Try
            If (Me.valida) Then
                If (validareplica().Rows.Count = 0) Then
                    objDAL = New ClassificacaoFiscalDAL
                    Me.clf_codigo = objDAL.inserir(Me.clf_descricao, Me.sta_codigo, usuario)
                Else : Throw New Exception("Classificação fiscal já cadastrada.")
                End If
            Else
                Throw New Exception("Preencha todos os campos.")
            End If
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
        If (Me.clf_codigo <= 0) Then
            Return False
        Else : Return True
        End If
    End Function

#End Region

#Region "Alterar"
    Function alterar(usuario As Integer) As Boolean
        Dim ret As Integer
        Try
            ret = -1
            If (Me.valida) Then
                If (validareplica().Rows.Count = 0) Then
                    objDAL = New ClassificacaoFiscalDAL
                    ret = objDAL.alterar(Me.clf_descricao, Me.sta_codigo, Me.clf_codigo, usuario)
                Else : Throw New Exception("Classificação fiscal já cadastrada.")
                End If
            Else
                Throw New Exception("Preencha todos os campos.")
            End If
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
        If (ret <= 0) Then
            Return False
        Else : Return True
        End If
    End Function
#End Region

#Region "Localizar"
    Public Function localizar(codigo As String, tipo As Integer) As DataTable
        Try
            objDAL = New ClassificacaoFiscalDAL
            Return objDAL.buscar(codigo, tipo)
        Catch ex As Exception
            Throw ex
        End Try
    End Function
#End Region

#Region "Validar Réplica de Categoria"
    Function validareplica() As DataTable
        Dim ret As DataTable
        Try

            ret = objfuncoes.ValidaReplica(Me.clf_descricao, Me.clf_codigo, "Estoque.Gen_ClaFiscal", "clf_codigo", "clf_descricao")
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
        Return ret
    End Function
#End Region

#Region "Valida Campos obrigatórios"
    Public Function valida() As Boolean
        If Me.clf_descricao.Trim = Nothing Or Me.clf_descricao.Trim = "" Then Throw New Exception("Preencha a descrição.")
        If Me.sta_codigo = Nothing Or Me.sta_codigo <= 0 Then Throw New Exception("Preencha o campo status.")
        Me.validarelacionamento()
        Return True
    End Function
#End Region

#Region "Valida Relacionamento"
    Public Function validarelacionamento() As Boolean
        If (Me.sta_codigo = 3 Or Me.sta_codigo = 2) Then
            objDAL = New ClassificacaoFiscalDAL
            If objDAL.verificarelacionamento(Me.clf_codigo) <> "" Then Throw New Exception("Esta classificação não pode ser modificada, verifique antes a ocorrência da mesma no formulário: " & objDAL.verificarelacionamento(Me.clf_codigo))
        End If
        Return True
    End Function
#End Region

#End Region

#Region "Procedimentos"
#Region "Carrega Propriedades"
    Sub carrega(ByVal codigo As Integer)
        Dim tab As DataTable
        Try
            objDAL = New ClassificacaoFiscalDAL
            tab = objDAL.buscar(codigo, 1)
            If (tab.Rows.Count > 0) Then
                Me.clf_codigo = Integer.Parse(tab.Rows(0)("clf_codigo").ToString)
                Me.clf_descricao = tab.Rows(0)("clf_descricao").ToString
                Me.sta_codigo = tab.Rows(0)("sta_codigo").ToString
            End If
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
    End Sub
#End Region

#Region "Limpa Propriedades"
    Sub limpar()
        Me.clf_codigo = Nothing
        Me.clf_descricao = Nothing
        Me.sta_codigo = Nothing
        Me.objDAL = Nothing
    End Sub
#End Region
#End Region


End Class
